import QuestSpawn from "../QuestSpawn";
import GameUtil from '../../Fantasy/Util/GameUtil';
import ServerCenter from '../../Server/ServerCenter';

const {ccclass, property} = cc._decorator;

const RobotAnswer = [
    [1,1,1,0,1,0,0,0,1,0],
    [1,1,1,1,1,1,0,1,0,1],
    [1,0,1,1,0,1,1,0,0,1],
    [1,1,1,1,1,1,0,0,0,0],
    [1,1,1,0,0,0,1,1,0,0]
]

@ccclass
export default class PKInfoJs extends cc.Component {

    @property(cc.Label)
    maxTime: cc.Label = null;
    @property(cc.Label)
    myExp: cc.Label = null;
    @property(cc.Label)
    Robot: cc.Label = null;
    @property(cc.Sprite)
    myProgress : cc.Sprite = null;
    @property(cc.Sprite)
    robotProgress : cc.Sprite = null;
    @property(cc.Prefab)
    selectQuest: cc.Prefab = null;
    @property(cc.Prefab)
    matchQuest: cc.Prefab = null;
    @property(cc.Prefab)
    WriteQuest: cc.Prefab = null;
    @property(cc.Node)
    qusetNode:cc.Node = null;

    time = 300;
    myCount = 0;
    myScore = 0;
    robotCout = 0;
    robotScore = 0;
    totalQuestNum = 10;
    currQuestNum = 0;
    questArr;
    expNum;
    interval;
    startGame = true;
    bookId;
    unit;
    onEnable(){
        this.time = 300;
        this.maxTime.string = '300秒';
        this.myExp.string = '0';
        this.Robot.string = '0';
        this.robotProgress.fillRange = 0;
        this.myProgress.fillRange = 0;
        this.robotCout = 0;
        this.robotScore = 0;
        this.myCount = 0;
        this.myScore = 0;
        this.startGame = true;

        QuestSpawn.setIsContest(true);
        QuestSpawn.setIsWrong(false);
        //生成题目
        this.currQuestNum = 0;
        this.initQuest(GameUtil.openBookId);
        // this.createRobot();
    }
    onDisable(){
        this.closeInterval()
    }
    initQuest(book){

        let quest = this.qusetNode.children;
        for(let node of quest){
            if (QuestSpawn.wrongArr.indexOf(node) < 0){
                node.destroy();
            }
            else{
                node.active = false;
                node.parent = null;
            }
        }
        this.currQuestNum ++;
        Log("题目数：",this.currQuestNum);
        if(this.currQuestNum == this.totalQuestNum){
            Log("游戏结束，加载闯关成功预制");
            this.GameOver();
            return;
        }

        if(book){
            this.bookId = book;
            let max= ServerCenter.getMaxLevel(GameUtil.openBookId);
            this.unit =  Math.ceil(Math.random()*max)
            this.questArr= QuestSpawn.getWordsInfo(book,this.unit);
            QuestSpawn.wrongArr = [];
        }
        let pross = this.currQuestNum/this.totalQuestNum;
        Log("jindu ",pross);
        if(pross < 0.85){
            let node = cc.instantiate(this.selectQuest);
            node.getComponent("SelectQuestJs").init(this,this.questArr,this.bookId,this.unit);
            node.parent = this.qusetNode;
        }else if(pross > 0.9){
            let node = cc.instantiate(this.WriteQuest);
            node.getComponent("WriteQuestJs").init(this,this.questArr);
            node.parent = this.qusetNode;
        }else if(pross >= 0.85){
            let node = cc.instantiate(this.matchQuest);
            node.getComponent("MacthQuestJs").init(this,this.questArr);
            node.parent = this.qusetNode;
        }

    }

    updateExp(number,bool){
        if(bool == false){
            this.answer(false,false);
            let quest = this.qusetNode.children;
            for(let i in quest){
                QuestSpawn.wrongArr.push(quest[i]);
                Log(QuestSpawn.wrongArr);
            }
        }else{
            this.answer(false,true);
        }
    }
    
    update(dt){
        if (this.startGame == false)
            return;
        this.time -= dt;
        this.maxTime.string = Math.ceil(this.time) + '秒';
        if (this.time <= 0){
            //强制结束
        }
    }
    /**
     * 调用答题
     * @param isRobot true为机器人 false为玩家本人
     * @param isOk true为答对,false为错误
     */
    answer(isRobot,isOk){
        if(isRobot){
            this.robotCout+=1;
            if (isOk){
                this.robotScore += 10;
            }
            else{
                this.robotScore -= 5;
            }
            if (this.robotCout >= 10){
                this.GameOver();
            }
            this.Robot.string = '' + this.robotScore;
            this.robotProgress.fillRange = this.robotCout / 10;
        }
        else{
            this.myCount+=1;
            if (isOk){
                this.myScore += 10;
            }
            else{
                    this.myScore -= 5;
                
            }
            if (this.myCount >= 10){
                this.GameOver();
            }
            this.myExp.string = '' + this.myScore;
            this.myProgress.fillRange = this.myCount / 10;
        }
        Log("玩家分数",this.myScore);
        Log('答题人:',isRobot?'机器人':'玩家','是否答对:',isOk);
    }
    GameOver(){
        Log('游戏结束')
        this.startGame = false;
        this.closeInterval();
        cc.find('Canvas').getComponent('PKScene').showGameOver(this.myScore,this.robotScore);
    }
    createRobot(){
        let index = Math.floor(Math.random()*RobotAnswer.length);
        let array = RobotAnswer[index];
        this.closeInterval();
        let time = 10;
        switch(GameUtil.currPkLevel){
            case 1:
                time = 10;
            case 2:
                time = 8;
            case 3:
                time = 6;
            case 4:
                time = 4;
        }
        let currIndex = 0;
        this.interval = setInterval(()=>{
            let ans = array[currIndex];
            let result = ans == 1 ? true : false;
            this.answer(true,result);
            currIndex += 1;
        },time*1000)
    }
    closeInterval(){
        if (this.interval){
            clearInterval(this.interval);
            this.interval = null;
        }
    }
}
